﻿ /**
*  eBook
*
*
*
*
*/
class Book
{
	//变量
	public var nowPage : Number = 0;
	public var width : Number = 300;
	public var height : Number = 400;
	private var tag : MovieClip;
	private var me : Object;
	private var page : Array;
	private var canShow : Boolean;
	//timeOutFun
	public function timeOutFun ()
	{
		say ("读取书页的时候超时！");
	}
	//构造函数
	public function Book (root : MovieClip)
	{
		if (root == undefined)
		{
			say ("未指定Book显示目标.");
			delete this;
		}else
		{
			init (root);
		}
	}
	/**
	*初始化函数
	*/
	private function init (root : MovieClip)
	{
		tag = root;
		//	tag._x=tag._y=-10000;
		me = this;
		canShow = false;
		//--------------
		nowPage = 0;
	}
	/*
	* 创建阴影
	*/
	private function createShade (stag : MovieClip)
	{
		trace(stag);
		stag.colors = [0, 0];
		stag.alphas = [30, 1];
		stag.ratios = [10, 0xFF];
		stag.matrix = {a:50, b:0, c:0, d:0, e:50, f:0, g:15, h:50, i:1};
		stag.beginGradientFill("linear", stag.colors, stag.alphas, stag.ratios, stag.matrix);
		stag.moveTo(0, 0);
		stag.lineTo(100, 0);
		stag.lineTo(100, 100);
		stag.lineTo(0, 100);
		stag.lineTo(0, 0);
		stag.endFill();
	}
	/*
	*初始化书页
	*/
	public function initPage (_page : Array)
	{
		if (_page.length > 0 || _page != undefined)
		{
			page = new Array ();
			_page.reverse ();
			for (var i in _page)
			{
				var o : Object = new Object ();
				o.path = _page [i];
				page.push (o);
			}
			reLoadPage ();
		}else
		{
			say ("书页未定义.");
		}
	}
	/*
	*加载页面
	*/
	public function loadPage ()
	{
		var d = tag.getNextHighestDepth ();
		if (d > 1020000)
		{
			d = 800000;
		}
		for (var i in page)
		{
			if (page [i].path != "" && page [i].path != undefined)
			{
				if (page [i].path.indexOf (".") != - 1 && typeof (page [i].path) != "movieclip")
				{
					//---是路径,要加载
					d = tag.getNextHighestDepth ();
					tag.createEmptyMovieClip ("page" + i, d);
					page [i].tag = tag ["page" + i];
					loadpage (tag ["page" + i] , page [i].path);
				} else if (typeof (page [i].path) == "movieclip")
				{
					//---是已经确定的页面
					
				} else if (typeof (page [i].path) == "string" && page [i].path.indexOf (".") == - 1)
				{
					//---link出来的页面
					d = tag.getBytesLoaded ();
					tag.attachMovie (page [i].path, "page" + i, d);
					page [i].tag = tag ["page" + i];
				}
				page [i].tag._x = page [i].tag._y = - 1000;
			}
		}
		createLoadingProcess ();
	}
	/*
	*重新加载页面
	*/
	public function reLoadPage ()
	{
		for (var i in page)
		{
			if (page [i].tag != undefined && typeof (page [i].tag) == "movieclip")
			{
				page [i].tag.removeMovieClip ();
			}
		}
		loadPage ();
	}
	//----------------------------  加载部分
	private function loadpage (tag : MovieClip, path : String)
	{
		tag.loadMovie (path);
	}
	private function createLoadingProcess ()
	{
		if (tag ["loading"] != undefined)
		{
			say ("已经有loading了.");
		}else
		{
			tag.createEmptyMovieClip ("loading", 1000000);
			var loading : MovieClip = tag ["loading"];
			loading.createTextField ("bfb", 1, - 30, 0, 30, 50);
			loading.bfb.autoSize = "right";
			loading.bfb.html = true;
			loading.bfb.htmlText = "0%";
			loading.tag = tag;
			loading.me = me;
			loading.timeOut = 200;
			loading.getTT = function ()
			{
				var rtTT : Number = 0;
				var page = me.page;
				for (var i in page)
				{
					if (page [i].path.indexOf (".") != - 1)
					{
						rtTT += page [i].tag.getBytesTotal ();
					}
				}
				return rtTT;
			}
			loading.getLD = function ()
			{
				var rtLD : Number = 0;
				var page = me.page;
				for (var i in page)
				{
					if (page [i].path.indexOf (".") != - 1)
					{
						rtLD += page [i].tag.getBytesLoaded ();
					}
				}
				return rtLD;
			}
			loading.createGraphic = function (x2 : Number)
			{
				if (this.ld == x2)
				{
					return;
				}
				var x = (x2 - this.ld) * 200;
				this.createEmptyMovieClip ("ld" + x2.toString () , this.getNextHighestDepth ());
				var t : MovieClip = this ["ld" + x2.toString ()];
				t.tag = this;
				t._alpha = 0;
				t._x = this.ld * 200;
				t._y = - 500;
				t.lineStyle (0, 0x999999, 100);
				t.beginFill (0x999999, 50);
				t.moveTo (0, 0);
				t.lineTo (x, 0);
				t.lineTo (x, 13);
				t.lineTo (0, 13);
				t.lineTo (0, 0);
				t.endFill ();
				t.y = 0;
				t.onEnterFrame = function ()
				{
					var ly : Number = this.y - this._y;
					if (Math.abs (ly) < 0.1)
					{
						this._y = this.y;
						this._alpha = 100;
						delete this.onEnterFrame;
					}else
					{
						this._alpha += 10;
						this._y += (ly) / 3;
					}
				}
				this.ld = x2;
			}
			loading.ld = 0;
			loading.slip = 30;
			loading._x = Stage.width / 2 - 100;
			loading._y = Stage.height / 2;
			loading.onEnterFrame = function ()
			{
				var tt : Number = this.getTT ();
				var ld : Number = this.getLD ();
				if (tt != undefined && tt.toString () != "NaN" && tt != 0)
				{
					if (tt != ld)
					{
						var bfbt : Number = int (ld * 10000 / tt) / 100;
						this.bfb.htmlText = "<font size='12' color='#999999'>" + bfbt.toString () + "%</font>";
						if (this.slip ++ > 50)
						{
							this.createGraphic (ld / tt);
							this.slip = 31;
						}
					}else
					{
						if (this.slip > 30)
						{
							this.slip = 30;
						}
						this.bfb.htmlText = "<font size='12' color='#999999'>100%</font>";
						this.createGraphic (ld / tt);
						if (this.slip > 0)
						{
							this.slip --;
						}else
						{
							this._alpha -= 5;
						}
						if (this._alpha <= 0)
						{
							this.me.show ();
							this.removeMovieClip ();
						}
					}
					this.timeOut = 200;
				}else
				{
					this.timeOut --;
				}
				if (this.timeOut < 0)
				{
					this.me.timeOutFun ();
				}
			};
		}
	}
	//-------------------------------------------
	//========================================================================= 以下为书页控制部分代码
	/*
	* 
	* 
	*/ 
	private function setPage(p:Number){
		
	}
	/*
	* 显示
	*/
	public function show ()
	{
		say ("显示");
	}
	//say
	public function say (o : Object)
	{
		trace ("Book: " + o);
	}
}
